//
//  THUD.swift
//  ClockWidget
//
//  Created by TSYH on 2024/3/8.
//

import UIKit
import ExtensionsKit
import SnapKit

public class THUD: NSObject {
    public enum `Position` {
        case top
        case center
        case bottom
    }
    
    public static let shared = THUD()
    private lazy var tipViews = [THUDViewProtocol]()
    
    private var currentViewController: UIViewController? {
        return UIApplication.rootViewController?.topController
    }
    
    @discardableResult
    public static func show(_ element: THUDProtocol,
                            position: THUD.Position,
                            in superView: UIView?) -> UIView? {
        let view = element.hudView
        // 去重
        guard !shared.tipViews.contains(where: { $0.hudIdentifier == view.hudIdentifier }) else {
            return view
        }
        shared.show(view: view, in: superView, at: position)
        
        if let duration = element.hudDuration, duration > 0 {
            shared.perform(#selector(hide(identifier:)), with: view.hudIdentifier, afterDelay: duration)
        }
        return view
    }
    
    public static func showTips(_ message: String,
                         in view: UIView? = nil,
                         style: TipsViewStyle = .normal,
                         position: `Position` = .center,
                         shakeType: UIImpactFeedbackGenerator.FeedbackStyle? = nil) {
        
        let tipsConfig = style.tipsConfig(message: message)
        show(tipsConfig, position: position, in: view)
        
        if let type = shakeType {
            UIImpactFeedbackGenerator(style: type).impactOccurred()
        }
    }
    
    public func show(view: THUDViewProtocol,
                     in superView: UIView?,
                     at position: `Position`) {
        let superView = superView ?? currentViewController?.view
        guard let superView = superView else { return }
        
        superView.addSubview(view)
        view.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            switch position {
            case .top:
                make.centerY.equalToSuperview().multipliedBy(0.3)
            case .center:
                make.centerY.equalToSuperview()
            case .bottom:
                make.centerY.equalToSuperview().multipliedBy(1.6)
            }
            if view.frame.size != .zero {
                make.size.equalTo(view.frame.size)
            }
        }
        tipViews.append(view)
    }
    
    public static func hide() {
        shared.tipViews.forEach({ $0.removeFromSuperview() })
        shared.tipViews.removeAll()
    }
    
    public static func hide(_ identifier: String) {
        shared.hide(identifier: identifier)
    }
}

// MARK: private Method
extension THUD {
    @objc func hide(identifier: String) {
        let views = tipViews.filter({ $0.hudIdentifier == identifier })
        views.forEach { view in
            view.removeFromSuperview()
            tipViews.removeAll(where: { $0 == view })
        }
    }
}
